package com.example.xpvideo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.xpvideo.entity.LsCustom;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.service.LsCustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author hjt
 * @create 2021-09-01 16:02
 * @describe:本地生活板块-自定义专区配置
 */
@RestController
@RequestMapping("custom")
public class CustomController {
    @Autowired
    private LsCustomService customService;

    /**
     * 修改自定义专区名称
     *
     * @param params
     * @return
     */
    @PutMapping("updateCustom")
    public ResponseResult updateCustom(@RequestBody Map<String, String> params) {
        return customService.updateCustom(params);
    }


    /**
     * 添加自定义专区中的套餐
     *
     * @param
     * @return
     */
    @PostMapping("addCombo")
    public ResponseResult addCombo(@Validated LsCustom custom, MultipartFile file, BindingResult result) {
        if (result.hasErrors()) {
            return ResponseResult.responseResult(ErrCodeEnum.unknown_10001, result.getTarget());
        }
        return customService.addCombo(custom, file);
    }

    /**
     * 删除套餐
     * @param id
     * @return
     */
    @DeleteMapping("delCombo/{id}")
    public ResponseResult delCombo(@PathVariable("id") Integer id) {
        return customService.delCombo(id);
    }

    /**
     * 编辑套餐
     *
     * @param custom
     * @return
     */
    @PutMapping("editCombo")
    public ResponseResult editCombo(@Validated LsCustom custom, @RequestParam(value = "file", required = false) MultipartFile file) {
        return customService.editCombo(custom, file);
    }

    /**
     * 根据专区查询套餐
     *
     * @param id
     * @return
     */
    @GetMapping("findCombo/{id}")
    public ResponseResult findCombo(@PathVariable("id") Integer id) {
        List<LsCustom> customList = customService.list(new QueryWrapper<LsCustom>().eq("parent_id", id));
        return ResponseResult.responseResult(0, customList);
    }

    /**
     * 查询自定义专区
     *
     * @param
     * @return
     */
    @GetMapping("findPrecinct")
    public ResponseResult findPrecinct() {
        List<LsCustom> customList = customService.list(new QueryWrapper<LsCustom>().isNull("parent_id").select("id", "name"));
        return ResponseResult.responseResult(0, customList);
    }

}
